package com.zlf.sb.demo;

/***
 *@title ShareDBTest
 *@description <TODO description class purpose>
 *@author wgc_j
 *@version 1.0.0
 *@create 2025-02-17 14:50
 **/
public class ShareDBTest {
    public static final int DB_CNT=16;
    public static final int TAB_CNT=100;
    //常用姿势一：标准的二次分片法
    public static  void  ShareDB(String userId){
         //算hash
        int hash=userId.hashCode();

        //总分片数
        int sumSlot= DB_CNT*TAB_CNT;
        //分配序号
        int  slot=Math.abs(hash%sumSlot);
        //
        int dbIndex=slot%TAB_CNT;
        System.out.println("dbIndex = " + dbIndex);
        int tabIndex=slot/TAB_CNT;
        System.out.println("tabIndex = " + tabIndex);
    }
    public static void main(String[] args) {
     //   ShareDB("123456");

        ShareDB2("123456");
    }

    //常用姿势二：关系表冗余
    public static  void  ShareDB2(String userId){
        int dbIndex = Math.abs(userId.substring(0,4).hashCode()%DB_CNT);
        int tbIndex = Math.abs(userId.hashCode()%TAB_CNT);
        System.out.println("dbIndex = " + dbIndex);

        System.out.println("tbIndex = " + tbIndex);
    }
}
